package com.ruibang.glass.sale.service;

import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class StringDGGeneratorService {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    private static final AtomicInteger counter = new AtomicInteger(1); // 每天开始时重置为1
    private static LocalDate lastDate = LocalDate.now().minusDays(1); // 假设上次调用是在一天前

    public static String generateString() {
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 如果日期有变化，重置计数器
        if (!today.equals(lastDate)) {
            counter.set(1);
            lastDate = today;
        }

        // 生成递增的4位数字
        int nextNumber = counter.getAndIncrement();
        String fourDigits = String.format("%04d", nextNumber); // 格式化数字为4位，不足部分用0填充

        // 拼接字符串
        String formattedDate = today.format(formatter);
        String result = "DG" + formattedDate + fourDigits;
        return result;
    }

}
